package au.com.iglootech.TaxTimeToolTip.ui.search;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.Task;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;
import au.com.iglootech.TaxTimeToolTip.service.TaskService;
import au.com.iglootech.component.IGTTable;

import com.vaadin.data.util.BeanContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction = true)
@Component
@Scope("request")
public class SearchTaskPage extends CustomComponent {
	VerticalLayout mainLayout = new VerticalLayout();
	@Autowired
	private transient TaskService taskService;
	@Autowired
	private transient CompanyService companyService;

	private static final String COMPANY_NAME = "company.companyName";

	private IGTTable taskTabel;
	private BeanContainer<String, Task> beans;
	private ComboBox companyComBox;
	private DateField startDate;
	private DateField endDate;

	public SearchTaskPage() {
		beans = new BeanContainer<String, Task>(Task.class);
		beans.setBeanIdProperty("key");
		beans.addNestedContainerProperty(COMPANY_NAME);
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();

		// company combobox
		List<Company> companies = companyService.findAllCompany();
		companyComBox = new ComboBox("Company");
		companyComBox.setImmediate(true);
		companyComBox.setNullSelectionAllowed(false);
		for (Company company : companies) {
			companyComBox.addItem(company);
			companyComBox.setItemCaption(company, company.getCompanyName());
		}
		startDate = new DateField("Start Date");
		startDate.setDateFormat("dd/MM/yyyy");
		endDate = new DateField("End Date");
		endDate.setDateFormat("dd/MM/yyyy");
		HorizontalLayout searchBar = new HorizontalLayout();
		searchBar.setSpacing(true);
		searchBar.addComponent(companyComBox);
		searchBar.addComponent(startDate);
		searchBar.addComponent(endDate);

		Button searchButton = new Button("Search");
		searchButton.addListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent arg0) {
				refreshTabel();
			}
		});

		searchBar.addComponent(searchButton);
		searchBar.setComponentAlignment(searchButton, Alignment.BOTTOM_RIGHT);
		panel.addComponent(searchBar);

		taskTabel = new IGTTable("", beans);
		taskTabel.setColumnHeader(COMPANY_NAME, "Company");
		taskTabel.setWidth("100%");
		taskTabel.setHeight("500px");
		taskTabel.setVisibleColumns(new String[] { "taskName",
				COMPANY_NAME, "period", "unit", "startDate", "invoiced" });
		panel.addComponent(taskTabel);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}

	private void refreshTabel() {
		List<Task> result = taskService.findTaskByCompany(
				(Company) companyComBox.getValue(),
				(Date) startDate.getValue(), (Date) endDate.getData(), null);
		beans.removeAllItems();
		for (Task task : result) {
			beans.addBean(task);
		}
	}
}